/**
 * 
 */
package com.hand.cloud.b2b.daos.impl;

import de.hybris.platform.catalog.enums.ArticleApprovalStatus;
import de.hybris.platform.catalog.model.CatalogModel;
import de.hybris.platform.catalog.model.CatalogVersionModel;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hand.cloud.b2b.daos.ProductDAO;
import com.hand.cloud.b2b.enums.EPProductOperation;
import com.hand.cloud.b2b.model.EPProductLogModel;


/**
 * @author StephenWang
 * @time Aug 14, 2014 1:36:37 PM
 * 
 */
@Component(value = "productDAO")
public class DefaultProductDAO implements ProductDAO
{

	public static Logger LOG = Logger.getLogger(DefaultProductDAO.class);

	@Autowired
	private ModelService modelService;

	@Autowired
	private FlexibleSearchService flexibleSearchService;

	/**
	 * 通过code获取商品信息
	 * 
	 * @param code
	 * @return
	 */
	@Override
	public ProductModel getProductByCode(String code)
	{

		ProductModel product = null;

		StringBuffer queryStr = new StringBuffer("");
		queryStr.append("select {p.pk} ").append("from {product as p join catalogversion as v on {p.catalogversion}={v.pk} ")
				.append("join catalog as c on {v.catalog}={c.pk}} ")
				.append("where {c.id} = 'electronicsProductCatalog' and {v.version}='Online' and {p.code} = ?code");

		FlexibleSearchQuery query = new FlexibleSearchQuery(queryStr.toString());
		query.addQueryParameter("code", code);

		LOG.info("----queryStr:" + query.getQuery());

		try
		{
			product = this.flexibleSearchService.searchUnique(query);
			LOG.info("-----prodcutPk:" + product.getPk());
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}

		return product;
	}

	/**
	 * 通过商品PK获取商品
	 * 
	 * @param pk
	 * @return
	 */
	@Override
	public ProductModel getProductByPK(String pk)
	{
		ProductModel product = null;

		StringBuffer queryStr = new StringBuffer("");
		queryStr.append("select {p.pk} ").append("from {product as p} ").append("where {p.pk} = ?pk");

		FlexibleSearchQuery query = new FlexibleSearchQuery(queryStr.toString());
		query.addQueryParameter("pk", pk);

		LOG.info("----queryStr:" + query.getQuery());

		try
		{
			product = this.flexibleSearchService.searchUnique(query);
			LOG.info("-----prodcutCode:" + product.getCode());
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}

		return product;

	}

	/**
	 * 添加一个商品
	 * 
	 * 
	 * @return
	 */
	@Override
	public ProductModel createProduct()
	{
		ProductModel product = this.modelService.create(ProductModel.class);
		product.setApprovalStatus(ArticleApprovalStatus.CHECK);

		CatalogModel catalog = new CatalogModel();
		catalog.setDefaultCatalog(Boolean.TRUE);
		catalog.setId("catalog1");

		CatalogVersionModel catalogversion = createCatalogVersion(catalog);

		product.setCatalogVersion(catalogversion);
		product.setCode("107701");

		try
		{
			this.modelService.save(product);
			// 添加日志 
			EPProductLogModel epProductLog = createEPProductLog("添加一条product记录");
			this.modelService.save(epProductLog);
		}
		catch (final Exception e)
		{
			e.printStackTrace();
			return null;
		}

		return product;
	}

	/**
	 * @param catalog
	 * @return
	 */
	private CatalogVersionModel createCatalogVersion(CatalogModel catalog)
	{
		CatalogVersionModel catalogversion = new CatalogVersionModel();
		catalogversion.setInclAssurance(Boolean.TRUE);
		catalogversion.setInclDuty(Boolean.TRUE);
		catalogversion.setInclFreight(Boolean.TRUE);
		catalogversion.setInclPacking(Boolean.TRUE);
		catalogversion.setVersion("catalogversion1");
		catalogversion.setCatalog(catalog);
		return catalogversion;
	}

	/**
	 * 通过商品pk删除一个商品
	 * 
	 * @param pk
	 * 
	 */
	@Override
	public void deleteProduct(String pk)
	{
		ProductModel product = getProductByPK(pk);

		try
		{
			this.modelService.remove(product);
			// 添加日志 
			EPProductLogModel epProductLog = createEPProductLog("删除一条product记录,pk为" + pk);
			this.modelService.save(epProductLog);
		}
		catch (final Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 修改一个商品
	 * 
	 * @param product
	 * 
	 */
	@Override
	public ProductModel modifyProduct(ProductModel product)
	{
		ProductModel productModel = getProductByPK(product.getPk().toString());
		productModel.setApprovalStatus(product.getApprovalStatus());

		try
		{
			this.modelService.save(productModel);
			// 添加日志 
			EPProductLogModel epProductLog = createEPProductLog("删除一条product记录,pk为" + product.getPk().toString());
			this.modelService.save(epProductLog);
		}
		catch (final Exception e)
		{
			e.printStackTrace();
			return null;
		}

		return productModel;
	}

	/**
	 * @param product
	 * @return
	 */
	private EPProductLogModel createEPProductLog(String raw)
	{
		EPProductLogModel epProductLog = this.modelService.create(EPProductLogModel.class);
		epProductLog.setOperation(EPProductOperation.DELETE);
		epProductLog.setRawString(raw);
		epProductLog.setTime(new Date());
		return epProductLog;
	}


}
